/*

    Copyright (C) 2010-2011 AmigaVR
    
    This file is part of AmigaVR.
    Written by Ketfi-Chérif Mehdi <ketficherif.mehdi@gmail.com>

    AmigaVR is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    AmigaVR is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with AmigaVR.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "global.h"

#if defined(_WAV_TEST_)
#include "wav.h"
#elif defined(_INTERRUPT_TEST_)
#include "inst.h"
uint8_t c = 0;
#endif

int
 main ( void )
{
#if defined(_WAV_TEST_)
	uint16_t i = 0, mode = 0;
	uint16_t end = sizeof ( data );
	dac_init ( DACB , FALSE , FALSE );
	while ( 1 )
	{
		switch ( mode )
		{
			case 0:
			if ( i+1 >=  end )
			{
				i = 0;
				_delay_ms ( 250 );
			}
			dac_write ( DACB , data[i] );
			break;
		}
		i++;
		_delay_us ( 20 );
	}
#elif defined(_INTERRUPT_TEST_)
	sys_set_rc32m();

	dac_init ( DACB , FALSE , FALSE );

	sei();

	timer_init ();

	while ( 1 );
#else
	/* Void */
#endif
	return 0;
}

#if defined(_INTERRUPT_TEST_)
ISR ( TCC0_OVF_vect , ISR_NAKED )
{
	/* Maximum cycles: 48. */
	asm volatile (
		/* 1 */	"push r16" 		"\n\t"
		/* 1 */	"push r17"		"\n\t"
		/* 1 */	"push r18"		"\n\t"
		/* 1 */	"push r26"		"\n\t"
		/* 1 */	"push r27"		"\n\t"
		/* 1 */	"in r16 , 0x3F"		"\n\t" /* Get the SREG reg.         */
		/* 1 */	"push r16"		"\n\t" /* Save it to the Stack      */
		/* 1 */	"ldi r16, 0x00" 	"\n\t"
		/* 2 */	"sts 0x0800 , r16" 	"\n\t" /* CTRLA = 0x00              */
		/* 2 */	"sts 0x0820 , r16"	"\n\t" /* CNTL = 0x00               */
		/* 2 */	"sts 0x0821 , r16"	"\n\t" /* CNTH = 0x00               */
		/* 3 */	"lds r18 , 0x2068"	"\n\t" /* r18 = counter             */
		/* 1 */	"ldi r26 , 0x28"	"\n\t" /* XL = low(instrument)      */
		/* 1 */	"ldi r27 , 0x20"	"\n\t" /* XH = high(instrument)     */
		/* 1 */	"add r26 , r18"		"\n\t"
		/* 1 */	"adc r27 , r16"		"\n\t" /* X = X+counter             */
		/* 1 */	"inc r18"		"\n\t" /* counter++                 */
		/* 2 */	"sts 0x2068 , r18"	"\n\t" /* save counter              */
		/* 1 */	"cpi r18 , 0x39"	"\n\t" /* counter == 0xFE ?         */
	        /* 2 */	"brne .+2"		"\n\t" 
		/* 2 */	"sts 0x2068 , r16"	"\n\t" /* Yes: counter = 0x00       */
		/* 2 */	"ld r17 , X"		"\n\t" /* r17 = instrument[counter] */
		/* 2 */	"sts 0x0338 , r17"	"\n\t" /* DACB.CH0DATAL = r17       */
		/* 2 */	"sts 0x0339 , r16"	"\n\t" /* DACB.CHODATAH = 0x00      */
		/* 1 */	"inc r16" 		"\n\t"
		/* 2 */	"sts 0x0800 , r16"	"\n\t" /* TCC0.CTRLA = 0x01         */
		/* 1 */	"pop r16"		"\n\t"
		/* 1 */	"out 0x3F, r16"		"\n\t"
		/* 1 */	"pop r27"		"\n\t"
		/* 1 */	"pop r26"		"\n\t"
		/* 1 */	"pop r18"		"\n\t"
		/* 1 */	"pop r17"		"\n\t"
		/* 1 */	"pop r16" 		"\n\t"
		/* 4 */	"reti"			"\n\t" 
		     );
}
#endif
